const security_flag = {};
security_flag.base_url = ""
security_flag.action_url = "/security/flag"
security_flag.errorMessage = null

security_flag.send = function(url, data, callback) {
    let request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState === 4) {
            let result = null;
            if (request.status === 200) {
                result = request.responseText ? request.responseText : null;
                if (result.indexOf('errCode') != -1) {
                    if (security_flag.errorMessage) {
                        security_flag.errorMessage(result)
                    } else {
                        console.error(result);
                    }
                } else {
                    if (callback) {
                        callback(result);
                    }
                }
            } else {
                console.error(request);
            }
        }
    }
    request.open("POST", security_flag.base_url + security_flag.action_url + url, true);
    request.responseType = "text";
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    request.send(data);
}

security_flag.submit = function(problemID, flag, callback) {
    let data = [];
    data.push("problemID=" + problemID);
    data.push("flag=" + flag);

    security_flag.send("/submit", data.join("&"), callback);
}
